home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / a_utils / perl / msds-prl / perl419x.zoo / perldb.pl < prev    next >
Perl Script  |  1993-07-25  |  8KB  |  259 lines

  1. package DB;
  2.  
  3. # modified Perl debugger, to be run from Emacs in perldb-mode
  4. # Ray Lischner (uunet!mntgfx!lisch) as of 5 Nov 1990
  5. # Johan Vromans -- upgrade to 4.0 pl 10
  6.  
  7. $header = '$RCSfile: perldb.pl,v $$Revision: 4.0.1.2 $$Date: 91/11/05 17:55:58 $';
  8. #
  9. # This file is automatically included if you do perl -d.
  10. # It's probably not useful to include this yourself.
  11. #
  12. # Perl supplies the values for @line and %sub.  It effectively inserts
  13. # a do DB'DB(<linenum>); in front of every place that can
  14. # have a breakpoint.  It also inserts a do 'perldb.pl' before the first line.
  15. #
  16. # $Log:    perldb.pl,v $
  17. # Revision 4.0.1.2  91/11/05  17:55:58  lwall
  18. # patch11: perldb.pl modified to run within emacs in perldb-mode
  19. # Revision 4.0.1.1  91/06/07  11:17:44  lwall
  20. # patch4: added $^P variable to control calling of perldb routines
  21. # patch4: debugger sometimes listed wrong number of lines for a statement
  22. # Revision 4.0  91/03/20  01:25:50  lwall
  23. # 4.0 baseline.
  24. # Revision 3.0.1.6  91/01/11  18:08:58  lwall
  25. # patch42: @_ couldn't be accessed from debugger
  26. # Revision 3.0.1.5  90/11/10  01:40:26  lwall
  27. # patch38: the debugger wouldn't stop correctly or do action routines
  28. # Revision 3.0.1.4  90/10/15  17:40:38  lwall
  29. # patch29: added caller
  30. # patch29: the debugger now understands packages and evals
  31. # patch29: scripts now run at almost full speed under the debugger
  32. # patch29: more variables are settable from debugger
  33. # Revision 3.0.1.3  90/08/09  04:00:58  lwall
  34. # patch19: debugger now allows continuation lines
  35. # patch19: debugger can now dump lists of variables
  36. # patch19: debugger can now add aliases easily from prompt
  37. # Revision 3.0.1.2  90/03/12  16:39:39  lwall
  38. # patch13: perl -d didn't format stack traces of *foo right
  39. # patch13: perl -d wiped out scalar return values of subroutines
  40. # Revision 3.0.1.1  89/10/26  23:14:02  lwall
  41. # patch1: RCS expanded an unintended $Header in lib/perldb.pl
  42. # Revision 3.0  89/10/18  15:19:46  lwall
  43. # 3.0 baseline
  44. # Revision 2.0  88/06/05  00:09:45  root
  45. # Baseline version 2.0.
  46. #
  47.  
  48. open(IN, "<CON") || open(IN,  "<&STDIN");    # so we don't dingle stdin
  49. open(OUT,">CON") || open(OUT, ">&STDOUT");    # so we don't dongle stdout
  50. select(OUT);
  51. $| = 1;                # for DB'OUT
  52. select(STDOUT);
  53. $| = 1;                # for real STDOUT
  54. $sub = '';
  55.  
  56. # Is Perl being run from Emacs?
  57. $emacs = $main'ARGV[$[] eq '-emacs';
  58. shift(@main'ARGV) if $emacs;
  59.  
  60. $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
  61. print OUT "\nLoading DB routines from $header\n";
  62. print OUT ("Emacs support ",
  63.        $emacs ? "enabled" : "available",
  64.        ".\n");
  65. print OUT "\nEnter h for help.\n\n";
  66.  
  67. sub DB {
  68.     &save;
  69.     ($package, $filename, $line) = caller;
  70.     $usercontext = '($@, $!, $[, $,, $/, $\) = @saved;' .
  71.     "package $package;";        # this won't let them modify, alas
  72.     local($^P) = 0;            # don't debug our own evals
  73.     local(*dbline) = "_<$filename";
  74.     $max = $#dbline;
  75.     if (($stop,$action) = split(/\0/,$dbline{$line})) {
  76.     if ($stop eq '1') {
  77.         $signal |= 1;
  78.     }
  79.     else {
  80.         $evalarg = "\$DB'signal |= do {$stop;}"; &eval;
  81.         $dbline{$line} =~ s/;9($|\0)/$1/;
  82.     }
  83.     }
  84.     if ($single || $trace || $signal) {
  85.     if ($emacs) {
  86.         print OUT "\032\032$filename:$line:0\n";
  87.     } else {
  88.         print OUT "$package'" unless $sub =~ /'/;
  89.         print OUT "$sub($filename:$line):\t",$dbline[$line];
  90.         for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) {
  91.         last if $dbline[$i] =~ /^\s*(}|#|\n)/;
  92.         print OUT "$sub($filename:$i):\t",$dbline[$i];
  93.         }
  94.     }
  95.     }
  96.     $evalarg = $action, &eval if $action;
  97.     if ($single || $signal) {
  98.     $evalarg = $pre, &eval if $pre;
  99.     print OUT $#stack . " levels deep in subroutine calls!\n"
  100.         if $single & 4;
  101.     $start = $line;
  102.       CMD:
  103.     while ((print OUT "  DB<", $#hist+1, "> "), $cmd=&gets) {
  104.         {
  105.         $single = 0;
  106.         $signal = 0;
  107.         $cmd eq '' && exit 0;
  108.         chop($cmd);
  109.         $cmd =~ s/\\$// && do {
  110.             print OUT "  cont: ";
  111.             $cmd .= &gets;
  112.             redo CMD;
  113.         };
  114.         $cmd =~ /^q$/ && exit 0;
  115.         $cmd =~ /^$/ && ($cmd = $laststep);
  116.         push(@hist,$cmd) if length($cmd) > 1;
  117.         ($i) = split(/\s+/,$cmd);
  118.         eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
  119.         $cmd =~ /^h$/ && do {
  120.             print OUT "
  121. T        Stack trace.
  122. s        Single step.
  123. n        Next, steps over subroutine calls.
  124. r        Return from current subroutine.
  125. c [line]    Continue; optionally inserts a one-time-only breakpoint 
  126.         at the specified line.
  127. <CR>        Repeat last n or s.
  128. l min+incr    List incr+1 lines starting at min.
  129. l min-max    List lines.
  130. l line        List line;
  131. l        List next window.
  132. -        List previous window.
  133. w line        List window around line.
  134. l subname    List subroutine.
  135. f filename    Switch to filename.
  136. /pattern/    Search forwards for pattern; final / is optional.
  137. ?pattern?    Search backwards for pattern.
  138. L        List breakpoints and actions.
  139. S        List subroutine names.
  140. t        Toggle trace mode.
  141. b [line] [condition]
  142.         Set breakpoint; line defaults to the current execution line; 
  143.         condition breaks if it evaluates to true, defaults to \'1\'.
  144. b subname [condition]
  145.         Set breakpoint at first line of subroutine.
  146. d [line]    Delete breakpoint.
  147. D        Delete all breakpoints.
  148. a [line] command
  149.         Set an action to be done before the line is executed.
  150.         Sequence is: check for breakpoint, print line if necessary,
  151.         do action, prompt user if breakpoint or step, evaluate line.
  152. A        Delete all actions.
  153. V [pkg [vars]]    List some (default all) variables in package (default current).
  154. X [vars]    Same as \"V currentpackage [vars]\".
  155. < command    Define command before prompt.
  156. > command    Define command after prompt.
  157. ! number    Redo command (default previous command).
  158. ! -number    Redo number\'th to last command.
  159. H -number    Display last number commands (default all).
  160. q or ^D        Quit.
  161. p expr        Same as \"print DB'OUT expr\" in current package.
  162. = [alias value]    Define a command alias, or list current aliases.
  163. command        Execute as a perl statement in current package.
  164.  
  165. ";
  166.             next CMD; };
  167.         $cmd =~ /^t$/ && do {
  168.             $trace = !$trace;
  169.             print OUT "Trace = ".($trace?"on":"off")."\n";
  170.             next CMD; };
  171.         $cmd =~ /^S$/ && do {
  172.             foreach $subname (sort(keys %sub)) {
  173.             print OUT $subname,"\n";
  174.             }
  175.             next CMD; };
  176.         $cmd =~ s/^X\b/V $package/;
  177.         $cmd =~ /^V$/ && do {
  178.             $cmd = 'V $package'; };
  179.         $cmd =~ /^V\b\s*(\S+)\s*(.*)/ && do {
  180.             $packname = $1;
  181.             @vars = split(' ',$2);
  182.             do 'dumpvar.pl' unless defined &main'dumpvar;
  183.             if (defined &main'dumpvar) {
  184.             &main'dumpvar($packname,@vars);
  185.             }
  186.             else {
  187.             print DB'OUT "dumpvar.pl not available.\n";
  188.             }
  189.             next CMD; };
  190.         $cmd =~ /^f\b\s*(.*)/ && do {
  191.             $file = $1;
  192.             if (!$file) {
  193.             print OUT "The old f command is now the r command.\n";
  194.             print OUT "The new f command switches filenames.\n";
  195.             next CMD;
  196.             }
  197.             if (!defined $_main{'_<' . $file}) {
  198.             if (($try) = grep(m#^_<.*$file#, keys %_main)) {
  199.                 $file = substr($try,2);
  200.                 print "\n$file:\n";
  201.             }
  202.             }
  203.             if (!defined $_main{'_<' . $file}) {
  204.             print OUT "There's no code here anything matching $file.\n";
  205.             next CMD;
  206.             }
  207.             elsif ($file ne $filename) {
  208.             *dbline = "_<$file";
  209.             $max = $#dbline;
  210.             $filename = $file;
  211.             $start = 1;
  212.             $cmd = "l";
  213.             } };
  214.         $cmd =~ /^l\b\s*(['A-Za-z_]['\w]*)/ && do {
  215.             $subname = $1;
  216.             $subname = "main'" . $subname unless $subname =~ /'/;
  217.             $subname = "main" . $subname if substr($subname,0,1) eq "'";
  218.             ($file,$subrange) = split(/:/,$sub{$subname});
  219.             if ($file ne $filename) {
  220.             *dbline = "_<$file";
  221.             $max = $#dbline;
  222.             $filename = $file;
  223.             }
  224.             if ($subrange) {
  225.             if (eval($subrange) < -$window) {
  226.                 $subrange =~ s/-.*/+/;
  227.             }
  228.             $cmd = "l $subrange";
  229.             } else {
  230.             print OUT "Subroutine $1 not found.\n";
  231.             next CMD;
  232.             } };
  233.         $cmd =~ /^w\b\s*(\d*)$/ && do {
  234.             $incr = $window - 1;
  235.             $start = $1 if $1;
  236.             $start -= $preview;
  237.             $cmd = 'l ' . $start . '-' . ($start + $incr); };
  238.         $cmd =~ /^-$/ && do {
  239.             $incr = $window - 1;
  240.             $cmd = 'l ' . ($start-$window*2) . '+'; };
  241.         $cmd =~ /^l$/ && do {
  242.             $incr = $window - 1;
  243.             $cmd = 'l ' . $start . '-' . ($start + $incr); };
  244.         $cmd =~ /^l\b\s*(\d*)\+(\d*)$/ && do {
  245.             $start = $1 if $1;
  246.             $incr = $2;
  247.             $incr = $window - 1 unless $incr;
  248.